import * as Koa from 'koa';

const app = new Koa();
const json = require('koa-json');
const onerror = require('koa-onerror');
const logger = require('koa-logger');

const bodyParser = require('koa-bodyparser');
const session = require('koa-session');
const convert = require('koa-convert');
const serve = require('koa-static');
import routeList from './controller'
import env from './config/env';
import { dingConfig } from 'dingtalk-app';
const path = require('path');
onerror(app);

// middlewares
app.use(json());
app.use(require('koa-static')(__dirname + '/public'));
app.use(logger());
app.use(bodyParser());
app.use(convert(session(app)));
app.use(serve(path.join(__dirname, 'public')));

//安全校验
dingConfig.init(app, env as any, [
  /^\/api\/*/
])

//加载路由
routeList().forEach(dir => {
  let route = require(dir);
  app.use(route.default.routes());
});

export default app;
